package com.moge.anjian.adapter;

import android.app.TaskInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.moge.anjian.R;
import com.moge.anjian.beans.PlayBackBean;

import java.util.ArrayList;
import java.util.List;

public class RecordListAdapter extends RecyclerView.Adapter<RecordListAdapter.MyViewHolder> {


    private Context mContext;
    private List<PlayBackBean> mList = new ArrayList<>();
    private RecordClickCallback mClickCallback;

    public void setClickCallback(RecordClickCallback clickCallback) {
        this.mClickCallback = clickCallback;
    }

    public RecordListAdapter(Context context, List<PlayBackBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_record, null, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvId.setText(position + 1 + "");
        holder.tvName.setText(mList.get(position).name);
        if (mClickCallback == null) {
            return;
        }
        holder.itemView.setOnClickListener(v -> mClickCallback.itemClick(mList.get(position)));
        holder.btnStart.setOnClickListener(v -> mClickCallback.start(mList.get(position)));
        holder.btnDelete.setOnClickListener(v -> mClickCallback.delete(mList.get(position)));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvId, tvName, tvDetails;
        public Button btnStart, btnDelete;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvId = itemView.findViewById(R.id.tv_id);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDetails = itemView.findViewById(R.id.tv_name);
            btnStart = itemView.findViewById(R.id.btn_start);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }

    public interface RecordClickCallback{
        void delete(PlayBackBean playBackBean);
        void start(PlayBackBean playBackBean);
        void itemClick(PlayBackBean playBackBean);
    }
}
